package cn.vw.hi.user.manager;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import cn.vw.hi.commen.tools.Page;
import cn.vw.hi.user.model.Ad;
import cn.vw.hi.user.model.WebsiteColumns;
import cn.vw.hi.user.model.WebsiteConfiger;
import cn.vw.hi.user.persistence.WebsiteConfigerDao;

public class WebsiteConfigerManagerImpl implements WebsiteConfigerManager {

	private WebsiteConfigerDao websiteConfigerDao=null;
	// 配置网站
	public boolean configWebsite(WebsiteConfiger configer,String path) {
		path = path.replaceAll("%20", " ").replace("/", File.separator)
				.replace("\\", File.separator);
		File file = new File(path);

		SAXReader reader = new SAXReader();
		reader.setEncoding("utf-8");
		String tags = null;
		Document doc;
		try {
			doc = reader.read(file);
			doc.selectSingleNode("//websites/websiteName").setText(configer.getWebsiteName());
			doc.selectSingleNode("//websites/keywords").setText(configer.getKeywords());
			doc.selectSingleNode("//websites/description").setText(configer.getDescription());
			doc.selectSingleNode("//websites/tel").setText(configer.getTel());
			doc.selectSingleNode("//websites/phone").setText(configer.getPhone());
			doc.selectSingleNode("//websites/email").setText(configer.getEmail());
			doc.selectSingleNode("//websites/address").setText(configer.getAddress());
			doc.selectSingleNode("//websites/copyright").setText(configer.getCopyright());
			doc.selectSingleNode("//websites/bgimg").setText(configer.getBgimg());
			doc.selectSingleNode("//websites/zoomLevel").setText(configer.getZoomLevel());
			doc.selectSingleNode("//websites/radius").setText(configer.getRadius());
			OutputFormat format = new OutputFormat("\t", true);
			format.setEncoding("utf-8");
			XMLWriter output = new XMLWriter(new FileOutputStream(
					new File(path)), format);
			output.write(doc);
			output.close();
			doc.clearContent();

		} catch (Exception e) {

			e.printStackTrace();
			return false;
		}
		return true;
	}
	/**
	 * 获得网站配制
	 */
	
	public WebsiteConfiger websiteConfig( String path) {
		WebsiteConfiger configer=new WebsiteConfiger();
    	path=path.replaceAll("%20"," ").replace("/", File.separator).replace("\\",File.separator);
    	File file=new File(path);
    	SAXReader reader = new SAXReader();
    	String tags=null;
    	Document doc;
    	try {
   		 doc = reader.read(file);
   		configer.setWebsiteName(doc.selectSingleNode("//websites/websiteName").getText().trim());
   		configer.setKeywords(doc.selectSingleNode("//websites/keywords").getText().trim());
   		configer.setDescription(doc.selectSingleNode("//websites/description").getText().trim());
   		configer.setTel(doc.selectSingleNode("//websites/tel").getText().trim());
   		configer.setPhone(doc.selectSingleNode("//websites/phone").getText().trim());
   		configer.setEmail(doc.selectSingleNode("//websites/email").getText().trim());
   		configer.setAddress(doc.selectSingleNode("//websites/address").getText().trim());
   		configer.setCopyright(doc.selectSingleNode("//websites/copyright").getText().trim());
   		configer.setBgimg(doc.selectSingleNode("//websites/bgimg").getText().trim());
   		configer.setZoomLevel(doc.selectSingleNode("//websites/zoomLevel").getText().trim());
   		configer.setRadius(doc.selectSingleNode("//websites/radius").getText().trim());
	   	} catch (DocumentException e) {
	   		e.printStackTrace();
	   	}
		return configer;
	}
	//添加栏目
	public boolean addWebsiteColumn(WebsiteColumns websiteColumns){
		
		return websiteConfigerDao.addWebsiteColumn(websiteColumns);
	}
	public WebsiteConfigerDao getWebsiteConfigerDao() {
		return websiteConfigerDao;
	}
	public void setWebsiteConfigerDao(WebsiteConfigerDao websiteConfigerDao) {
		this.websiteConfigerDao = websiteConfigerDao;
	}
	//获得网站列表
	public List websiteColumnList(Page pageObj) {
		return websiteConfigerDao.websiteColumnList(pageObj);
	}
	//添加网站栏目
	public boolean modifyWebsiteColumn(WebsiteColumns websiteColumns) {
		return websiteConfigerDao.modifyWebsiteColumn(websiteColumns);
	}
	//获得栏目
	public WebsiteColumns findWebsiteColumn(Integer id) {
		return websiteConfigerDao.findWebsiteColumn(id);
	}
	//添加广告图片
	public boolean adAd(Ad ad, String path) {
		path = path.replaceAll("%20", " ").replace("/", File.separator)
		.replace("\\", File.separator);
			File file = new File(path);
			
			SAXReader reader = new SAXReader();
			reader.setEncoding("utf-8");
			String tags = null;
			Document doc;
			try {
				doc = reader.read(file);
				Element root = doc.getRootElement();
				Element adE = root.addElement("ad");
				adE.addAttribute("id", ad.getId()+"");
				
				Element title = adE.addElement("title");
				title.setText(ad.getTitle());
				
				Element imgage = adE.addElement("image");
				imgage.setText(ad.getImage());
				
				Element url = adE.addElement("url");
				url.setText(ad.getUrl());
				
				OutputFormat format = new OutputFormat("\t", true);
				format.setEncoding("utf-8");
				
				XMLWriter output = new XMLWriter(new FileOutputStream(new File(path)), format);
				output.write(doc);
				output.close();
				doc.clearContent();
			
			} catch (Exception e) {
			
				e.printStackTrace();
				return false;
			}
			return true;
	}
	//广告图片
	@SuppressWarnings("unchecked")
	public List adList(String path) {
		List adList=new ArrayList();
    	path=path.replaceAll("%20"," ").replace("/", File.separator).replace("\\",File.separator);
    	File file=new File(path);
    	SAXReader reader = new SAXReader();
    	Document doc;
    	try {
   		 doc = reader.read(file);
   		List rowList = doc.selectNodes("//ads/ad");
   		for(int i=0;i<rowList.size();i++)
   		{
   			Element item = (Element) rowList.get(i);   
   			String id = item.attributeValue("id");    
   			Ad ad=new Ad();
   			   ad.setId(Integer.parseInt(id));
   			  ad.setTitle(item.elementText("title").toString());
   			ad.setUrl(item.elementText("url").toString());
   			ad.setImage(item.elementText("image").toString());
   			adList.add(ad);
   		}
		
	   	} catch (DocumentException e) {
	   		e.printStackTrace();
	   	}
		return adList;
	}
	//删除Ad
	public boolean deleteAd(int id,String path) {
		path=path.replaceAll("%20"," ").replace("/", File.separator).replace("\\",File.separator);
    	File file=new File(path);
    	SAXReader reader = new SAXReader();
    	Document doc;
    	try {
   		 doc = reader.read(file);
   		List rowList = doc.selectNodes("//ads/ad");
   		Element root =doc.getRootElement();
   		for(int i=0;i<rowList.size();i++)
   		{
   			Element item = (Element) rowList.get(i);   
   			String tempid = item.attributeValue("id"); 
   			if(Integer.parseInt(tempid)==id)
   			{
   				root.remove(item);
   				break;
   			}
   		}
   		OutputFormat format = new OutputFormat("\t", true);
		format.setEncoding("utf-8");
		
		XMLWriter output = new XMLWriter(new FileOutputStream(new File(path)), format);
		output.write(doc);
		output.close();
		doc.clearContent();
	   	} catch (Exception e) {
	   		e.printStackTrace();
	   		return false;
	   	}
		return true;
	}
	//修改ad
	public boolean modifyAd(Ad ad, String path) {
		path=path.replaceAll("%20"," ").replace("/", File.separator).replace("\\",File.separator);
    	File file=new File(path);
    	SAXReader reader = new SAXReader();
    	Document doc;
    	try {
   		 doc = reader.read(file);
   		List rowList = doc.selectNodes("//ads/ad");
   		Element root =doc.getRootElement();
   		for(int i=0;i<rowList.size();i++)
   		{
   			Element item = (Element) rowList.get(i);   
   			String tempid = item.attributeValue("id"); 
   			if(Integer.parseInt(tempid)==ad.getId())
   			{
   				item.selectSingleNode("title").setText(ad.getTitle());
   				item.selectSingleNode("url").setText(ad.getUrl());
   				item.selectSingleNode("image").setText(ad.getImage());
   			}
   		}
   		OutputFormat format = new OutputFormat("\t", true);
		format.setEncoding("utf-8");
		
		XMLWriter output = new XMLWriter(new FileOutputStream(new File(path)), format);
		output.write(doc);
		output.close();
		doc.clearContent();
	   	} catch (Exception e) {
	   		e.printStackTrace();
	   		return false;
	   	}
		return true;
	}
}
